package com.ruoyi.functiontest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.InterchangeMileageStatTestMapper;
import com.ruoyi.functiontest.domain.InterchangeMileageStatTest;
import com.ruoyi.functiontest.service.IInterchangeMileageStatTestService;

/**
 * 互通里程实测数据测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class InterchangeMileageStatTestServiceImpl implements IInterchangeMileageStatTestService 
{
    @Autowired
    private InterchangeMileageStatTestMapper interchangeMileageStatTestMapper;

    /**
     * 查询互通里程实测数据测试
     * 
     * @param id 互通里程实测数据测试主键
     * @return 互通里程实测数据测试
     */
    @Override
    public InterchangeMileageStatTest selectInterchangeMileageStatTestById(Long id)
    {
        return interchangeMileageStatTestMapper.selectInterchangeMileageStatTestById(id);
    }

    /**
     * 查询互通里程实测数据测试列表
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 互通里程实测数据测试
     */
    @Override
    public List<InterchangeMileageStatTest> selectInterchangeMileageStatTestList(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.selectInterchangeMileageStatTestList(interchangeMileageStatTest);
    }

    /**
     * 新增互通里程实测数据测试
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 结果
     */
    @Override
    public int insertInterchangeMileageStatTest(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.insertInterchangeMileageStatTest(interchangeMileageStatTest);
    }

    /**
     * 修改互通里程实测数据测试
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 结果
     */
    @Override
    public int updateInterchangeMileageStatTest(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.updateInterchangeMileageStatTest(interchangeMileageStatTest);
    }

    /**
     * 批量删除互通里程实测数据测试
     * 
     * @param ids 需要删除的互通里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeMileageStatTestByIds(Long[] ids)
    {
        return interchangeMileageStatTestMapper.deleteInterchangeMileageStatTestByIds(ids);
    }

    /**
     * 删除互通里程实测数据测试信息
     * 
     * @param id 互通里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeMileageStatTestById(Long id)
    {
        return interchangeMileageStatTestMapper.deleteInterchangeMileageStatTestById(id);
    }

    @Override
    public boolean hasChildById(Long id)
    {
        return interchangeMileageStatTestMapper.hasChildById(id) > 0;
    }
    
    @Override
    public String importInterchangeMileageStatTest(List<InterchangeMileageStatTest> importList, boolean updateSupport)
    {
        if (importList == null || importList.isEmpty()) {
            return "导入数据为空";
        }
        
        int successCount = 0;
        int failureCount = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        // 构建父子关系映射
        java.util.Map<Long, InterchangeMileageStatTest> idMap = new java.util.HashMap<>();
        java.util.Map<String, InterchangeMileageStatTest> keyMap = new java.util.HashMap<>();
        
        // 第一遍：收集所有数据到映射中
        for (InterchangeMileageStatTest data : importList) {
            if (data.getId() != null) {
                idMap.put(data.getId(), data);
            }
            // 创建业务键：父节点ID + 路段名称 + 收费站互通名称
            String businessKey = (data.getParentId() != null ? data.getParentId() : "null") + "_" + 
                               (data.getRoadName() != null ? data.getRoadName() : "null") + "_" + 
                               (data.getNodeName() != null ? data.getNodeName() : "null");
            keyMap.put(businessKey, data);
        }
        
        // 第二遍：处理导入数据
        for (InterchangeMileageStatTest data : importList) {
            try {
                // 检查数据是否已存在（通过ID或业务键）
                InterchangeMileageStatTest existingData = null;
                
                if (data.getId() != null) {
                    // 通过ID检查
                    existingData = interchangeMileageStatTestMapper.selectInterchangeMileageStatTestById(data.getId());
                }
                
                if (existingData == null) {
                    // 通过业务键检查
                    InterchangeMileageStatTest query = new InterchangeMileageStatTest();
                    query.setParentId(data.getParentId());
                    query.setRoadName(data.getRoadName());
                    query.setNodeName(data.getNodeName());
                    
                    List<InterchangeMileageStatTest> existingList = interchangeMileageStatTestMapper.selectInterchangeMileageStatTestExactMatch(query);
                    if (existingList != null && !existingList.isEmpty()) {
                        existingData = existingList.get(0);
                    }
                }
                
                if (existingData != null) {
                    if (updateSupport) {
                        // 更新现有数据
                        data.setId(existingData.getId());
                        interchangeMileageStatTestMapper.updateInterchangeMileageStatTest(data);
                        successMsg.append("<br/>" + data.getNodeName() + " 更新成功");
                    } else {
                        failureMsg.append("<br/>" + data.getNodeName() + " 已存在");
                        failureCount++;
                        continue;
                    }
                } else {
                    // 插入新数据
                    interchangeMileageStatTestMapper.insertInterchangeMileageStatTest(data);
                    successMsg.append("<br/>" + data.getNodeName() + " 导入成功");
                }
                successCount++;
            } catch (Exception e) {
                failureCount++;
                String errorMsg = data.getNodeName() != null ? data.getNodeName() : "未知数据";
                failureMsg.append("<br/>" + errorMsg + " 导入失败：" + e.getMessage());
            }
        }
        
        if (failureCount > 0) {
            failureMsg.insert(0, "，失败 " + failureCount + " 条，导入失败信息：");
        }
        return "导入结果：共 " + importList.size() + " 条，成功 " + successCount + " 条" + failureMsg;
    }
    
    @Override
    public int updateReview1ToZero()
    {
        return interchangeMileageStatTestMapper.updateReview1ToZero();
    }
    
    @Override
    public boolean checkAllReview1IsZero()
    {
        return interchangeMileageStatTestMapper.checkAllReview1IsZero() == 0;
    }
    
    @Override
    public int updateReview2ToZeroByCondition(InterchangeMileageStatTest queryCondition)
    {
        return interchangeMileageStatTestMapper.updateReview2ToZeroByCondition(queryCondition);
    }
    
    @Override
    public boolean checkAllReview1AndReview2AreZero()
    {
        return interchangeMileageStatTestMapper.checkAllReview1AndReview2AreZero() == 0;
    }
    
    @Override
    public int updateReview3ToZero()
    {
        return interchangeMileageStatTestMapper.updateReview3ToZero();
    }
}
